package com.cdd.user.controller;

import com.cdd.user.service.UserService;
import com.cdd.user.vo.UserVo;
import com.common.entity.Role;
import com.common.entity.User;
import com.common.untils.CommonResult;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 *
 *
 * @author lenovo
 * @version 1.0
 * @since 2022-09-03  18:49:53
 */
@RestController
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    //查询全部
    @PostMapping("/userAll/{currentPage}/{pageSize}")
    public CommonResult userAll(@PathVariable Integer currentPage, @PathVariable Integer pageSize, @RequestBody UserVo userVo){
        return userService.userAll(currentPage,pageSize,userVo);
    }
    //删除员工
    @PostMapping("/deleteUserById/{userId}")
    public CommonResult deleteUserById(@PathVariable String userId){
        return userService.deleteUserById(userId);
    }
    //添加员工
    @PostMapping("/addUserByUser")
    public CommonResult addUserByUser(@RequestBody User user){

        return userService.addUserByUser(user);
    }
    //修改状态
    @PostMapping("/updateUserState/{userId}/{state}")
    public CommonResult updateUserState(@PathVariable String userId,@PathVariable Integer state){
        return userService.updateUserState(userId,state);
    }
    //修改员工
    @PostMapping("/updateUser")
    public CommonResult updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }
    //设置角色
    @PostMapping("/findRoleByUser/{userId}")
    public CommonResult findRoleByUser(@PathVariable String userId){
        return userService.findRoleByUser(userId);
    }
    //修改密码
    @PostMapping("/updateUserPassword")
    public CommonResult updateUserPassword(@RequestBody Map updatePasswordDate, HttpServletRequest request){
        String token = request.getHeader("token");
        return userService.updateUserPassword(updatePasswordDate,token);
    }
    //分页查询所有
    @PostMapping("/findRoleByRoleName/{roleCurrentPage}/{rolePageSize}")
    public CommonResult findRoleByRoleName(@PathVariable("roleCurrentPage") Integer roleCurrentPage,@PathVariable("rolePageSize") Integer rolePageSize,@RequestBody Role role){
        return userService.findRoleByRoleName(roleCurrentPage,rolePageSize,role);
    }
    //退出
    @PostMapping("loginout")
    public CommonResult loginout(HttpServletRequest request){
        String token = request.getHeader("token");
        Boolean delete = redisTemplate.delete(token);
        if (delete){
            return new CommonResult(2000,"退出登录成功",null);
        }
        return new CommonResult(5000,"退出登录失败",null);
    }
}
